jQuery(document).ready(function() {
    
    var uploaded_id = 'edit-field-uploaded-und-0-upload';
    jQuery('#'+uploaded_id).addClass("required"); 
    //file upload onchange
    jQuery('#' + uploaded_id).live('change',function(){
        //console.log('change');
        if(jQuery('div.form-item-field-uploaded-und-0').parent().parent().find('div.messages').length){
            jQuery('div.form-item-field-uploaded-und-0').parent().parent().find('div.messages').remove();
        }
      
    });
                
    var upload_error = Drupal.t('Please upload the attachment before you submit document .');
    //Validate set start
    jQuery.extend(jQuery.validator.messages,{
        required: Drupal.t("This field is required.")
    });
    //End validate set
   var check_file_upload =  function(){
        // must allow file before submit form
        var upload_file_id = 'edit-field-uploaded-und-0-upload';
        if(jQuery('#'+upload_file_id).length){
            var error = '<label id="upload-file-error" for="edit-field-uploaded-und-0-upload" generated="true" class="error" style="display: block;">'+ upload_error + '</label>';
            if(!(jQuery('#upload-file-error').length)){
                jQuery("#edit-field-uploaded-und-0-upload").parent().append(error);
            }
            else{
                jQuery('#upload-file-error').show();			
            }
            if(!jQuery('#' + upload_file_id).hasClass('error')){
                jQuery('#' + upload_file_id).addClass('error');				 
            }
            jQuery('#' + upload_file_id).focus();
            return false;
        }
        return true;
    };

  jQuery("#hr-document-node-form").validate({
        errorPlacement: function(error, element) {
            if ( element.attr("id") == 'edit-field-uploaded-und-0-upload' ){
                error.insertAfter( element.next() );
            }else
                error.insertAfter(element);
        },
        //stone added
        submitHandler: function(form){
            if(check_file_upload()){
                form.submit();
            }
        }
    });
    jQuery("#employee-document-node-form").validate({
        errorPlacement: function(error, element) {
            if ( element.attr("id") == 'edit-field-uploaded-und-0-upload' ){
                error.insertAfter( element.next() );
            }else
                error.insertAfter(element);
        },
        //stone added
        submitHandler: function(form){
            if(check_file_upload()){
                form.submit();
            }
        }
    });
});